package sdufe.lele.controller;


import org.apache.shiro.util.ThreadContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sdufe.lele.annotation.authToken;
import sdufe.lele.exception.ApiException;
import sdufe.lele.dao.UserinfoMapper;
import sdufe.lele.model.Userinfo;
import sdufe.lele.result.*;
import sdufe.lele.service.RedisDateTimeService;
import sdufe.lele.uitl.Token;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Created by Olivia on 2017/4/5.
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserinfoMapper userinfoMapper;

    @Resource
    private RedisDateTimeService redisDateTimeService;




    @RequestMapping("/login.do")
    public Result login(@RequestParam(name = "userphone") String userPhone,
                        @RequestParam(name = "password") String password,
                        @RequestParam(name = "rem_pass") String rem_pass) {
        MapResult mapResult = new MapResult();
        Userinfo userinfo = userinfoMapper.selectByPhone(userPhone);

        if (!userinfo.getUserpassword().equals(password)) {
            throw new ApiException(401, "wrong password or phone", "");
        } else {
            mapResult.put("token", Token.createToken("userphone",userPhone,"rem-pass",rem_pass));
        }
        redisDateTimeService.setByKey(userPhone, LocalDateTime.now());
        return mapResult;
    }

    @RequestMapping("/register.do")
    public Result register(@RequestParam(name = "username") String userName,
                           @RequestParam(name = "userage") String userAge,
                           @RequestParam(name = "userphone") String userPhone,
                           @RequestParam(name = "useremail") String userEmail,
                           @RequestParam(name = "password") String password) {
        MapResult mapResult = new MapResult();
        Userinfo userinfo = new Userinfo(userName, userAge, userPhone, userEmail, password, 1);

        userinfoMapper.insertSelective(userinfo);

        mapResult.put("token", Token.createToken("userphone",userPhone,"rem-pass","yes"));
        redisDateTimeService.setByKey(userPhone, LocalDateTime.now());

        return mapResult;
    }

    @RequestMapping("/logout.do")
    public Result logout (){
        ApiResult apiResult = new ApiResult();

        redisDateTimeService.deleteByKey((String) ThreadContext.get("user-phone"));
        return apiResult;
    }
    @GetMapping("/isadmin")
    @authToken
    public Result isAdmin(){
        ApiResult apiResult = new ApiResult();
        String userPhone = (String) ThreadContext.get("user-phone");
        if (userPhone.equals("admin"))
            return apiResult;
        return new ApiError();

    }

    @RequestMapping("/verify.do")
    public Result verify(@RequestParam(name = "key") String key,
                         @RequestParam(name = "value") String value) {
        StringResult stringResult = new StringResult();

        if (key.equals("phone")) {
            Userinfo userinfo = userinfoMapper.selectByPhone(value);
            if (userinfo == null && value.length() == 11) {
                stringResult.setMessage("此手机号可以使用！");
                return stringResult;
            }
            throw new ApiException(403, "手机号不合法或手机号已被注册", "no");
        }

        if (key.equals("email")) {
            Userinfo userinfo = userinfoMapper.selectByEmail(value);

                if (userinfo == null && value.contains("@")) {
                stringResult.setMessage("此邮箱可以使用！");
                return stringResult;
            }
            throw new ApiException(403, "邮箱不合法或邮箱已经注册", "no");
        }

        if (key.equals("age")) {
            int age = Integer.parseInt(value);
            if (age >= 0 && age < 150){
                stringResult.setMessage("年龄符合范围");
                return stringResult;
            }
            throw new ApiException(403, "年龄不符合范围", "no");

        }




        throw new ApiException(403, "违规请求", "no");

    }

    @GetMapping("/allInfo.do")
    @authToken
    public Result allInfo(){
    ListResult listResult = new ListResult();
        List<Userinfo> userinfos = userinfoMapper.selectNormal();
        listResult.setAll(userinfos);
        return listResult;
    }

}
